/**
 * 
 */
package info.niwota.start.app;

import info.niwota.start.Debug;
import info.niwota.start.ImageUtil;
import info.niwota.start.R;
import info.niwota.start.ResourceInfo;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;

/**
 * @author qiangli
 * 
 */
public class ChoiceListAdapter extends BaseAdapter {
	private static class ViewHolder {
		private ImageView icon;
		private CheckedTextView title;
	}

	private Activity ctx;
	private ArrayList<ResourceInfo> list;
	private static final String TAG = "ChoiceListAdapter";

	/**
	 * 
	 */
	public ChoiceListAdapter(Activity ctx, ArrayList<ResourceInfo> list) {
		this.ctx = ctx;
		this.list = list;
	}

	public int getCount() {
		return (list == null ? 0 : list.size());
	}

	public ResourceInfo getItem(int position) {
		if (list == null) {
			return null;
		}
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	public void refresh() {
		notifyDataSetChanged();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (Debug.DEBUG) {
			Log.d(TAG, "getView position: " + position);
		}
		ViewHolder holder;
		if (convertView == null) {
			LayoutInflater li = LayoutInflater.from(ctx);
			convertView = li.inflate(R.layout.app_list_item_multiple_choice, null);
			holder = new ViewHolder();
			holder.title = (CheckedTextView) convertView.findViewById(android.R.id.text1);
			holder.title.setCheckMarkDrawable(R.drawable.star_big);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		ResourceInfo item = getItem(position);
		holder.title.setText(item.getTitle());
		Drawable d = ImageUtil.filter(item.getIcon()); // 48x48
		holder.icon.setImageDrawable(d);

		return convertView;
	}

}
